package com.MonkeyFlash;

import net.rim.device.api.system.CodeModuleGroup;
import net.rim.device.api.system.CodeModuleGroupManager;
import net.rim.device.api.ui.UiApplication;
import blackberry.util.Permissions;

import com.MonkeyFlash.option.model.HomeScreenHotkeyOption;

public class MonkeyFlashLauncher extends UiApplication {

	private static final String GROUP_NAME = "MonkeyFlash";
	public static final String[] MODULE_NAMES = new String[] {
		"MonkeyFlash",
		"MonkeyFlashLauncher"
	};
	
	public static void main(String[] args) {

		if (args != null && args.length > 0 && args[0].equals("startup")) {
			initialHotkey();
			setModuleGroup();
			new MonkeyListener().enterEventDispatcher();
		} else {
			if (isDenyPermissions())
				return;
			new MonkeyFlash().enterEventDispatcher();
		}
	}

	private static void initialHotkey() {
		HomeScreenHotkeyOption.getIns().updateHomeScreenHotkeyWithPersistentData();
	}
	
	private static void setModuleGroup(){
		if (CodeModuleGroupManager.load(GROUP_NAME) != null) 
			return;
		CodeModuleGroup group = new CodeModuleGroup(GROUP_NAME);
		for (int i = 0; i < MODULE_NAMES.length; i++) 
			group.addModule(MODULE_NAMES[i]);
		group.store();
	}
	
	private static boolean isDenyPermissions() {
		if (Permissions.isDenyPermissions(OtherInfo.REQUEST_PERMISSIONS) == false) 
			return false;
		
		Permissions.invokePermissionsRequest(OtherInfo.REQUEST_PERMISSIONS);
		
		return Permissions.isDenyPermissions(OtherInfo.REQUEST_PERMISSIONS) ? true : false;
	}
}
